import { _decorator, Color, color, math, js, log, warn, error } from 'cc';
import { DEBUG, JSB } from 'cc/env';

export type LogWithTagInnerData = {
    _tag: String,
    _tag_color: String,
    _logBackground: String,
    _logColor: String
}

export class LogWithTag {

    _lwt: LogWithTagInnerData = {
        _tag: '',
        _tag_color: '',
        _logBackground: '',
        _logColor: ''
    };

    get logEnable(): boolean {
        return true;
    }

    get logColorOverride(): Color {
        return color().fromHEX('#C8C8C8');
    }

    logInit?() {
        let color = this.logColorOverride;
        this._lwt._logBackground = color.toHEX("#rrggbb");
        if (color.r + color.g + color.b < 255 * 3 / 2) {
            this._lwt._logColor = math.color(Math.max(0, color.r + 100), Math.max(0, color.g + 100), Math.max(0, color.b + 100)).toHEX("#rrggbb");
        } else {
            this._lwt._logColor = math.color(Math.max(0, color.r - 100), Math.max(0, color.g - 100), Math.max(0, color.b - 100)).toHEX("#rrggbb");
        }
        this.logCreateTag();
    }

    logCreateTag?() {
        this._lwt._tag = `%c<${js.getClassName(this)}>`;
        this._lwt._tag_color = '';
        if (this._lwt._logBackground && this._lwt._logBackground != 'ffffff') this._lwt._tag_color += `background:#${this._lwt._logBackground};`;
        if (this._lwt._logColor && this._lwt._logColor != '000000') this._lwt._tag_color += `color:#${this._lwt._logColor};`;
    }

    makeTag?(array) {
        if (DEBUG) {
            if (JSB) {
                array.unshift(`[${js.getClassName(this)}]`);
            } else {
                if (this._lwt._logBackground == '' && this._lwt._logColor == '') {
                    array.unshift(this._lwt._tag);
                } else {
                    array.unshift(this._lwt._tag_color);
                    array.unshift(this._lwt._tag);
                }
            }
        }
    }

    debug(...param) {
        if (DEBUG && this.logEnable) {
            let _temp = Array.from(arguments);
            _temp.unshift('*');
            this.makeTag(_temp);
            console.debug.apply(this, _temp);
        }
    }

    log(...param) {
        if (DEBUG && this.logEnable) {
            let _temp = Array.from(arguments);
            this.makeTag(_temp);
            log.apply(this, _temp);
        }
    }

    warn(...param) {
        if (DEBUG) {
            let _temp = Array.from(arguments);
            this.makeTag(_temp);
            warn.apply(this, _temp);
        }
    }

    error(...param) {
        if (DEBUG) {
            let _temp = Array.from(arguments);
            this.makeTag(_temp);
            error.apply(this, _temp);
        }
    }

    // time(label: string) {
    //     if (DEBUG) {
    //         console.time.apply(this, [label]);
    //     }
    // }

    // timeLog(key: string, ...param) {
    //     if (DEBUG) {
    //         let _temp = Array.from(param);
    //         this.makeTag(_temp);
    //         console.timeLog.apply(this, [key, ..._temp]);
    //     }
    // }
}

